Skip to content

fix(android): keep image attachment state writes on the UI thread - #295

Merged
sambitcreate merged 1 commit into
mainfrom
fix/android-carousel-flake
Sep 30, 2026
Merged

sambitcreate merged 1 commit into
mainfrom
fix/android-carousel-flake

Conversation

@sambitcreate

@sambitcreate sambitcreate commented Sep 30, 2026 •

Copy link
Copy Markdown
Owner

Flake

AidenImageCarouselUiTest > cardDeckSwipesOneImageAndOpensTheSelectedGalleryPage failed once on hosted CI with IllegalArgumentException: performMeasureAndLayout called during measure layout and passed on rerun:
https://github.com/sambitcreate/aiden-agent/actions/runs/36656165396/job/109700927204

Root cause

I reproduced it locally on an API 36 emulator (about 1 in 40 single-test runs). The full trace shows the UI thread's ordinary Choreographer → ViewRootImpl.performDraw → AndroidComposeView.dispatchDraw → measureAndLayout finding duringMeasureLayout already set. That flag is cleared in a finally, so a second thread was inside a measure/layout pass on the same root. In the captured run, the crash happened during the first captureToImage, while the deck's images were still decoding.

AidenAttachmentImage's LaunchedEffect decodes on Dispatchers.Default and then writes the bitmap/failed state in the continuation. It relied on the host effect dispatcher to bring it back to the main thread. That holds in production (AndroidUiDispatcher), but the Compose test rule (v1, ui-test 1.10.6) uses an UnconfinedTestDispatcher for effects. The logs confirmed that the continuation resumed on DefaultDispatcher-worker-N. The rule's ApplyingContinuationInterceptor then sends snapshot apply notifications from that worker, and that lets recomposition and measure/layout run off the main thread, racing the UI thread's draw.

Fix (production code)

  • The image-load effect body now runs under withContext(Dispatchers.Main.immediate). Loading and decoding still hop to background dispatchers, but the continuation and the state writes always land on the UI thread. In production this changes nothing, because effects already run on the main thread and immediate doesn't dispatch.
  • The gallery save coroutine is pinned the same way, because it toggles saving and shows a Toast after an IO hop.

The test itself is unchanged: no retries, sleeps, timeouts, or ignores.

Verification

  • Before the fix: 2 crashes locally with the same trace (1 in 40 runs, then 1 in 39 runs).
  • After the fix: temporary logging showed the continuation resuming on main. The single test then passed 150 of 150 runs with the emulator's CPU under extra load. The full connectedDebugAndroidTest suite also passed (27 tests, 0 failures).
  • :app:testDebugUnitTest passes.

🤖 Generated with Claude Code

AidenAttachmentImage's LaunchedEffect decodes on Dispatchers.Default and
then writes `bitmap`/`failed` in the continuation. It relied on the host
effect dispatcher to hop back to the main thread. The Compose test rule's
effect dispatcher is an UnconfinedTestDispatcher, so the continuation
resumed on a DefaultDispatcher worker. The rule's
ApplyingContinuationInterceptor then sent snapshot apply notifications
from that worker, which let recomposition/measure run off the main
thread while the UI thread was drawing. The result was an intermittent
"performMeasureAndLayout called during measure layout" crash in
AidenImageCarouselUiTest.

Run the effect body under Dispatchers.Main.immediate, which is a no-op
in production where effects already run on the UI thread, so the load
and decode still happen off-main but the state writes happen on it. Apply the
same pinning to the gallery save coroutine, which toggles `saving` and
shows a Toast after an IO hop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@very-hermes-bot

Copy link
Copy Markdown
Collaborator

Hermes Review Bot

Confidence: 5

Engine: agy/gemini-3.8-flash-high
Review mode: full
Head: fb9c7445c11b2b7a45ed5e4cb1850ce7713f99bc
Generated: 2026-09-30T19:43:25+00:00
Reviews: 1

Summary

This change pins Compose state mutations and UI notifications in Android image attachment handling to the main UI thread. In AidenAttachmentImage, wrapping the load and decode effect body in withContext(Dispatchers.Main.immediate) guarantees that coroutine resumptions returning from AidenAttachmentBitmapCache.decode (Dispatchers.Default) execute on the UI thread rather than the test runner's UnconfinedTestDispatcher, preventing background snapshot mutations from triggering layout passes while the UI thread is drawing. Similarly, AidenAttachmentGallery.performSave explicitly launches on Dispatchers.Main.immediate so that saving state updates and the completion Toast are always posted to the main looper following background I/O. Maintainers should double-check that all loadData providers (notably viewModel::attachmentImageData in AidenChatDetailScreen.kt:703) continue to offload disk and network operations asynchronously so calling loadData inside withContext(Dispatchers.Main.immediate) never blocks the UI thread.

Confidence Score: 5/5

Fully traced the coroutine resumption paths and Compose snapshot threading behavior across android/app/src/main/java/sbtbiswas/AidenOnTheGo/features/chat/AidenMessageImageAttachments.kt, call sites in AidenChatDetailScreen.kt, and the instrumented AidenImageCarouselUiTest.kt suite.

📁 Important Files Changed
  • android/app/src/main/java/sbtbiswas/AidenOnTheGo/features/chat/AidenMessageImageAttachments.kt: Encloses LaunchedEffect state resets and post-decode state mutations within withContext(Dispatchers.Main.immediate), and scopes performSave coroutines to Dispatchers.Main.immediate to ensure saving mutations and Toast display run on the Android UI thread.

Findings

No findings.

Sequence Diagram

Unchanged or not applicable for this change.

[]

Last reviewed commit: fb9c7445c11b
Reviews (1) · Comment /hermes review to trigger a new review · /hermes review full for full re-review

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes This initial review covers the Android image-load and gallery-save dispatcher changes.

  • Image-load state updates The load/decode continuation returns to Dispatchers.Main.immediate before updating Compose state, while the existing IO and Default dispatcher hops remain intact.
  • Gallery save state updates The save coroutine runs on Dispatchers.Main.immediate, keeping the post-IO state update and Toast on the UI thread.

Pullfrog  | View workflow run | Using GPT Luna | 𝕏

@sambitcreate
sambitcreate merged commit 7ef01af into main Sep 30, 2026
19 checks passed
@sambitcreate
sambitcreate deleted the fix/android-carousel-flake branch September 30, 2026 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants